#include "MySqlConnPool/include/mysql.h"

sql::Driver* mysql::driver=nullptr;

mysql::mysql()
{
    if(driver==nullptr)
    {
        driver=sql::mysql::get_driver_instance();
    }
    res_is_empty=true;
}

void mysql::connectToSql(const std::string address, const std::string user, const std::string password, const std::string dbname)
{
    conn=driver->connect(address,user,password);
    conn->setSchema(dbname);
    stmt=conn->createStatement();
}

void mysql::executeVoid(std::string query)
{
    stmt->execute(query);
}

sql::ResultSet *mysql::execute(std::string query)
{
    res=stmt->executeQuery(query);
    checkRes();
    return res;
}

void mysql::executeVoid(std::string query, std::vector<std::string> &args)
{
    pre_stmt=conn->prepareStatement(query);
    int size=args.size();
    for(int i=0;i<size;i++)
    {
        pre_stmt->setString(i+1,args[i]);
    }
    pre_stmt->execute();
    delete pre_stmt;
}

sql::ResultSet *mysql::preExecute(std::string query, std::vector<std::string> &args)
{
    pre_stmt=conn->prepareStatement(query);
    int size=args.size();
    for(int i=0;i<size;i++)
    {
        pre_stmt->setString(i+1,args[i]);
    }
    res=pre_stmt->executeQuery();
    checkRes();
    return res;
}

sql::ResultSet *mysql::preExecute(std::string query, std::string &arg1)
{
    pre_stmt=conn->prepareStatement(query);
    pre_stmt->setString(1,arg1);
    res=pre_stmt->executeQuery();
    checkRes();
    return res;
}

void mysql::preExecuteVoid(std::string query, std::string &arg1)
{
    pre_stmt=conn->prepareStatement(query);
    pre_stmt->setString(1,arg1);
    pre_stmt->execute();
}

void mysql::preExecuteVoid(std::string query, std::string &arg1, std::string &arg2)
{
    pre_stmt=conn->prepareStatement(query);
    pre_stmt->setString(1,arg1);
    pre_stmt->setString(2,arg2);
    pre_stmt->execute();
}

sql::ResultSet* mysql::preExecute(std::string query, std::string &arg1, std::string &arg2)
{
    pre_stmt=conn->prepareStatement(query);
    pre_stmt->setString(1,arg1);
    pre_stmt->setString(2,arg2);
    res=pre_stmt->executeQuery();
    checkRes();
    return res;
}

void mysql::preExecuteVoid(std::string query)
{
    pre_stmt=conn->prepareStatement(query);
    pre_stmt->execute();
}

void mysql::preExecuteVoid(std::string query, std::vector<std::string> &args)
{
    pre_stmt=conn->prepareStatement(query);
    int size=args.size();
    for(int i=0;i<size;i++)
    {
        pre_stmt->setString(i+1,args[i]);
    }
    pre_stmt->execute();
}

void mysql::startTransaction()
{
    conn->setAutoCommit(false);
}

void mysql::commit()
{
    conn->commit();
    conn->setAutoCommit(true);
}

void mysql::disconnect()
{
    conn->close();
    delete stmt;
}

bool mysql::resIsEmpty()
{
    return res_is_empty;
}

void mysql::freeRes()
{
    if(res)
        delete res;
    res=nullptr;
}

void mysql::setCurDB(std::string db_name)
{
    conn->setSchema(db_name);
}

void mysql::resetResPos()
{
    res->absolute(1);
}

void mysql::setResPos(unsigned int pos)
{
    res->absolute(pos);
}

void mysql::checkRes()
{
    res_is_empty=!res->absolute(1);
    if(!res_is_empty)
        res->beforeFirst();
}
